package com.qyl.deguru.dao.impl;


import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.qyl.deguru.common.PageModel;
import com.qyl.deguru.dao.BaseDao;
import com.qyl.deguru.dao.ShopTypeDao;
import com.qyl.deguru.dto.Shop;
import com.qyl.deguru.dto.ShopType;
import com.qyl.deguru.mapper.CommentMapper;
import com.qyl.deguru.mapper.OrderItemMapper;
import com.qyl.deguru.mapper.ShopMapper;
import com.qyl.deguru.mapper.ShopTypeMapper;
import com.qyl.deguru.mapper.TimeLimitedMapper;
public class ShopTypeDaoImpl extends BaseDao implements ShopTypeDao {

	@Override
	public int getcount() {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		int recordcount=shopTypeMapper.getcount();
		closeSqlSession();
		return recordcount;
	}

	@Override
	public List<ShopType> showShopTypeList(ShopType shopType,
			PageModel pageModel) {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		List<ShopType> shopTypes=shopTypeMapper.showShopTypeList(shopType,pageModel);
		closeSqlSession();
		return shopTypes;
	}

	@Override
	public int delShopType(String code) {
		SqlSession session=getSqlSession();
		ShopMapper shopMapper=session.getMapper(ShopMapper.class);
		int[] shop_id=shopMapper.getIdByCode(code);
		System.out.println("\n"+shop_id+"=========");
		if(shop_id.length!=0){
			CommentMapper commentMapper=session.getMapper(CommentMapper.class);
			commentMapper.delCommentsByGoods_id(shop_id);
			OrderItemMapper orderItemMapper=session.getMapper(OrderItemMapper.class);
			orderItemMapper.delOrderItemsByGoods_id(shop_id);
			TimeLimitedMapper timeLimitedMapper=session.getMapper(TimeLimitedMapper.class);
			timeLimitedMapper.delTimeLimitedsByGoods_id(shop_id);
		}
		//删除商品类型数据，应先将商品数据中有关该类型的数据删除，才能删除数据
		shopMapper.delShopByType_code(code);
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		int count=shopTypeMapper.delShopType(code);
		session.commit();
		closeSqlSession();
		return count;
	}
	
	@Override
	public int delShopTypes(String[] ids) {
		SqlSession session=getSqlSession();
		//删除商品类型数据，应先将商品数据中有关该类型的数据删除，才能删除数据
		ShopMapper shopMapper=session.getMapper(ShopMapper.class);
//		SELECT   * FROM e_goods WHERE type_code IN (000010001,00030001)
		int[] shop_ids=shopMapper.getIdsByCode(ids);
		System.out.println("\n"+shop_ids+"=========");
		if(shop_ids.length!=0){
			CommentMapper commentMapper=session.getMapper(CommentMapper.class);
			commentMapper.delCommentsByGoods_id(shop_ids);
			OrderItemMapper orderItemMapper=session.getMapper(OrderItemMapper.class);
			orderItemMapper.delOrderItemsByGoods_id(shop_ids);
			TimeLimitedMapper timeLimitedMapper=session.getMapper(TimeLimitedMapper.class);
			timeLimitedMapper.delTimeLimitedsByGoods_id(shop_ids);
		}
		shopMapper.delShopsByType_code(ids);
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		int count=shopTypeMapper.delShopTypes(ids);
		session.commit();
		closeSqlSession();
		return count;
	}

	@Override
	public ShopType getShopTypeByCode(String code) {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		ShopType shopType=shopTypeMapper.getShopTypeByCode(code);
		closeSqlSession();
		return shopType;
	}

	@Override
	public int updateShopType(ShopType shopType) {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		int count=shopTypeMapper.updateShopType(shopType);
		session.commit();
		closeSqlSession();
		return count;
	}

	@Override
	public int addShopType(ShopType shopType) {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		int count =shopTypeMapper.addShopType(shopType);
		session.commit();
		closeSqlSession();
		return count;
	}

	@Override
	public List<Map<String, String>> getTypeList() {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		List<Map<String, String>> shopTypeList=shopTypeMapper.getTypeList();
		closeSqlSession();
		return shopTypeList;
	}

	@Override
	public List<ShopType> getShopTypeLimit4() {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		List<ShopType> shopType=shopTypeMapper.getShopTypeLimit4();
		closeSqlSession();
		return shopType;
	}
	@Override
	public List<ShopType> getShopTypeLimit8(String tcode) {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		List<ShopType> shopType=shopTypeMapper.getShopTypeLimit8(tcode);
		closeSqlSession();
		return shopType;
	}

	@Override
	public String[] getShopeCode(String tcode) {
		SqlSession session=getSqlSession();
		ShopTypeMapper shopTypeMapper=session.getMapper(ShopTypeMapper.class);
		String[] codes=shopTypeMapper.getShopeCode(tcode);
		closeSqlSession();
		return codes;
	}






	

}
